//
//  TVCommodityModel.swift
//  StarTimesON
//
//  Created by Toj on 12/6/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

struct TVCommodityModel: HandyJSON, DataDeserializer {
    var id = ""
    var amount: Double = 0
    var discount: TVCommodityDiscountModel?
    var numbers: [Int] = []
    //var couponCodes:
    var selected = false
    
    var additionalRuleId = ""
    var display_name = ""
    var amount_cny: Double = 0
    var currency_info: TVCurrencyInfo?
    var selected_number_index = 0
}

class TVCommodityCellModel: NSObject {
    
    var isSelected = false
    // 优惠券使用前价钱
    var startPrice: Double = 0
    // 优惠券使用后
    var endPrice: Double = 0
    
    var offPrice: Double {
        guard startPrice > endPrice else { return 0}
        return startPrice - endPrice
    }
    
    var selectedCouponModel: TVCouponCellModel?
    
    private(set) var id = ""
    private(set) var additionalRuleId = ""
    private(set) var name = ""
    
    // 货币编码, e.g.: "NGN"
    private(set) var currencyCode = ""
    // 货币符号, e.g.: "₦"
    private(set) var currencySymbol = ""
    
    // 现价(折扣价)
    private(set) var price: Double = 0
    // 现价(折扣价): 带货币符号
    private(set) var priceString: NSAttributedString?
    // 原价(吊牌价)
    private(set) var listPrice: Double = 0
    // 原价(吊牌价): 带货币符号
    private(set) var listPriceString: NSAttributedString?
    
    private(set) var numbers: [Int] = []
    var selectedNumberIndex = -1 { didSet {
        guard numbers.count > selectedNumberIndex, selectedNumberIndex >= 0 else { return }
        selectedNumberValue = numbers[selectedNumberIndex]
    } }
    private(set) var selectedNumberValue = 0
    private(set) var maxNumberValue = 0
    private(set) var maxNumberPrice: Double = 0
    
    private(set) var isDiscount = false
    private(set) var discountDescription = ""
    private(set) var discountStatus = 0
    
    init(commodity: TVCommodityModel) {
        id = commodity.id
        // 加购ID
        additionalRuleId = commodity.additionalRuleId
        isSelected = commodity.selected
        
        // 显示标题
        name = commodity.display_name
        
        // 货币
        currencyCode = commodity.currency_info?.currency_code ?? ""
        currencySymbol = commodity.currency_info?.currency_symbol ?? ""
        
        // 折扣价
        price = commodity.amount
        // 吊牌价
        listPrice = commodity.amount
        
        // 数量
        numbers = commodity.numbers
        selectedNumberIndex = commodity.selected_number_index
        if numbers.count > selectedNumberIndex {
            selectedNumberValue = numbers[selectedNumberIndex]
        }
        
        if let discount = commodity.discount {
            isDiscount = true
            discountDescription = discount.description
            discountStatus = discount.status
            price = discount.discount_amount
            listPrice = discount.original_amount
        }
        maxNumberValue = numbers.last ?? 0
        maxNumberPrice = Double(maxNumberValue) * price
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        paragraphStyle.alignment = .center
        let fontColor = "#554422".to_color
        var attributed = currencySymbol.to_attributedString(font: UIFont.boldSystemFont(ofSize: 10), color: fontColor, paragraph: paragraphStyle)
        let priceAttributed_ = price.st_moneyFormat.st_cleanZero.to_attributedString(font: UIFont.boldSystemFont(ofSize: 21), color: fontColor, paragraph: paragraphStyle)
        attributed.append(priceAttributed_)
        priceString = attributed
        
        guard listPrice > price else { return }
        // 原价
        let invalidMoney = currencySymbol + listPrice.st_moneyFormat.st_cleanZero
        attributed = invalidMoney.to_attributedString(font: 14.ui_systemFont, color: "#AAAAAA".to_color, paragraph: paragraphStyle, lineStyle: .strikethrough)
        listPriceString = attributed
    }
}
